package com.chazz.dao.impl;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.chazz.dao.DiscountDao;
import java.sql.SQLException;
import java.util.List;

/**
 * description: DiscountDaoImpl <br> date: 2020/9/22 9:49 <br> author: Chazz <br> version: 1.0 <br>
 */
public class DiscountDaoImpl implements DiscountDao {

    /**
     * description: selectDiscountById 实现父类的方法，通过商品的类别id查询折扣数 <br>
     * version: 1.0 <br>
     * date: 2020/9/2210:35 <br>
     * author: Chazz <br>
     *  @param categoryId 商品类别id
     * @return double 该商品的折扣数
     */
    @Override
    public double selectDiscountById(Integer categoryId) {
        double discount = 0;
        try {
            //通过hutools的query方法查询数据
            List<Entity> query = Db.use()
                .query("select type from discount where categoryid = ?", categoryId);
            //如果query中有数据，说明该商品有折扣
            if (query.size() >= 1) {
                discount = (double) query.get(0).get("type");
                System.out.println(discount);
            }//若没有折扣，则将折扣赋值为1，表示没有折扣
            else {
                discount = 1.0;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return discount;
    }
}

